# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=miller
pkgver=6.9.0
pkgrel=1
pkgdesc='Name-indexed data processing tool'
arch=('loong64' 'x86_64')
url='https://github.com/johnkerl/miller'
license=('BSD')
depends=('glibc')
makedepends=('go' 'git')
options=('!lto')
_commit='80bb82df6b7ab5e4ed3b3633574bdea60db05c1b'
source=("$pkgname::git+$url#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS}" \
    ./cmd/mlr
}

check() {
  cd "$pkgname"

  go test -v ./internal/pkg/...
  go test -v ./regression_test.go
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" mlr

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/man/man1" man/mlr.1
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE.txt
}
